/*
 * @(#)com.isoftstone.cloud.core.utils
 * @(#)RedisClient2.java	2016年10月15日
 * 
 * Copyright © 2001-2012, All rights reserved.
 * iSoftStone Information Technology ( Group ) Co., Ltd.
 */
package com.zhyonk.util;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * <P>Description: redis连接工具类</P>
 * @author zfm
 */
public class RedisClient {
    /**池化管理jedis链接池*/
    public static JedisPool jedisPool;

    static {


        Properties prop = new Properties();
        try {
            prop.load(RedisClient.class.getResourceAsStream("/redis.properties"));
            int maxActive = Integer.parseInt(prop.getProperty("redis.pool.maxActive"));
            int maxIdle = Integer.parseInt(prop.getProperty("redis.pool.maxIdle"));
            int maxWait = Integer.parseInt(prop.getProperty("redis.pool.maxWait"));
            String password = prop.getProperty("redis.pool.password");
            int timeOut = Integer.parseInt(prop.getProperty("redis.pool.timeout"));
            String ip = prop.getProperty("redis.pool.ip");
            int port = Integer.parseInt(prop.getProperty("redis.pool.port"));

            JedisPoolConfig config = new JedisPoolConfig();
            // 设置最大连接数
            config.setMaxTotal(maxActive);
            // 设置最大空闲数
            config.setMaxIdle(maxIdle);
            // 设置超时时间
            config.setMaxWaitMillis(maxWait);

            // 初始化连接池
            jedisPool = new JedisPool(config, ip, port, timeOut, password);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getLocalizedMessage());
        }
    }

    /**
     * 向缓存中设置字符串内容
     *
     * @param key   key
     * @param value value
     * @return
     * @throws Exception
     */
    public static boolean set(String key, String value) throws Exception {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 向缓存中设置对象
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean set(String key, Object value) {
        Jedis jedis = null;
        try {
            String objectJson = JSON.toJSONString(value);
            jedis = jedisPool.getResource();
            jedis.set(key, objectJson);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }


    /**
     * 向redis中缓存字节数组
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean set(byte[] key, byte[] value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 从redis缓存获取字节数组
     * @param key
     * @return
     */
    public static byte[] get(byte[] key) {
        Jedis jedis = null;
        byte[] bytes = null;
        try {

            jedis = jedisPool.getResource();
            bytes = jedis.get(key);
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
    /**
     * 设置byte[]类型key过期时间
     *
     * @param key
     * @param seconds
     * @return
     */
    public static boolean expire(byte[] key, int seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(key)) {
                jedis.expire(key, seconds);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }


    /**
     *判断redis缓存中是否存在byte[]类型的key
     * @param key
     * @return boolean
     */
    public static boolean exists(byte[] key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }



    public static boolean expire(String key, int seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(key)) {
                jedis.expire(key, seconds);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 删除缓存中得对象，根据key
     *
     * @param key
     * @return
     */
    public static boolean del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 根据key 获取内容
     *
     * @param key
     * @return
     */
    public static boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 根据key 获取内容
     *
     * @param key
     * @return
     */
    public static Object get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Object value = jedis.get(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 根据key 获取对象
     *
     * @param key
     * @return
     */
    public static <T> T get(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            return JSON.parseObject(value, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /*@Autowired
    private RedisTemplate<String, String> redisCacheTemplate;
    List<String> list = redisCacheTemplate.opsForList().range(RECEIVE_KEY,0,-1);
                if(list!=null && !list.isEmpty()){
        if(!list.contains(phone)){
            redisCacheTemplate.opsForList().rightPush(RECEIVE_KEY,phone);
        }
    }else{
        redisCacheTemplate.opsForList().rightPush(RECEIVE_KEY,phone);
    }

    String value = stringRedisTemplate.opsForValue().get("ORDERCODE_TIME");*/

    /*package com.example.huaxueshiyan.utils;

import sun.misc.BASE64Encoder;

    public class ImageUtils {
        *//**
         * 图片转base64编码
         *//*
        public static String toBase64(byte[] imageByte){
            BASE64Encoder base64Encoder=new BASE64Encoder();
            return base64Encoder.encode(imageByte);
        }

    }*/

}
